/*
The MIT License (MIT)

Copyright (c) 2014 firendlys.All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

License:	http://opensource.org/licenses/MIT
Project:	http://firendlys-projs.googlecode.com/svn/
**/


#include <myFn13/myStringRegex.h>
#include <regex>
using namespace std;
namespace myFn
{
	namespace v1
	{




		//------------------------------------------------
		//-- strMatchEx
		//-- 
		bool strMatchEx(const std::string &str,const std::string &regex_rule,bool isFullMatch,std::string flag)
		{
			if (str.empty() || regex_rule.empty())
			{
				return false;
			}
			try
			{
			const std::regex pattern(regex_rule);
			if (isFullMatch)
			{
				return std::regex_match(str, pattern);
			}
			else
			{
				//std::smatch result;
				return std::regex_search(str, pattern);
			}
			}
			catch(...)
			{
				return false;
			}
		}
		bool strMatchEx(cchar *str, cchar *regex_rule,bool isFullMatch, std::string flag )
		{
			if (!str || !regex_rule)
			{
				return false;
			}
			try
			{
				const std::regex pattern(regex_rule);
				if (isFullMatch)
				{
					return std::regex_match(str, pattern);
				}
				else
				{
					//std::smatch result;
					return std::regex_search(std::string(str), pattern);
				}
			}
			catch (...)
			{
				return false;
			}
		}
	
	
	
		string strReplaceEx(std::string str, std::string search_str, std::string replace_str, std::string flag /* = "" */)
		{
			if (str.empty() || search_str.empty() )
			{
				return str;
			}
			try
			{
				const std::regex pattern(search_str);
				return std::regex_replace(str, pattern, replace_str);
			}catch(...)
			{
				return str;
			}
		}
		string strReplaceAllEx(std::string str, std::string search_str, std::string replace_str, std::string flag /* = "" */)
		{
			if (str.empty() || search_str.empty())
			{
				return str;
			}
			try
			{
				const std::regex pattern(search_str);
				while (true)
				{
					auto s2 = std::regex_replace(str, pattern, replace_str);
					if (s2 != str)
					{
						str = s2;
						continue;
					}
					return s2;
				}
			}
			catch (...)
			{
				return str;
			}
		}

	
	
	
	
	
	};
};